/*
 * PanoramaGL library
 * Version 0.1
 * Copyright (c) 2010 Javier Baez <javbaezga@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.panorama.xtian;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import mapviewballoons.example.custom.CustomBalloonOverlayView;

import com.apps.essentials.AlertDialogManager;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.HomeActivity;
import com.apps.realtyportal.R;
import com.panoramagl.PLCubicPanorama;
import com.panoramagl.PLCylindricalPanorama;
import com.panoramagl.PLILoader;
import com.panoramagl.PLIPanorama;
import com.panoramagl.PLIView;
import com.panoramagl.PLImage;
import com.panoramagl.PLJSONLoader;
import com.panoramagl.PLSpherical2Panorama;
import com.panoramagl.PLSphericalPanorama;
import com.panoramagl.PLView;
import com.panoramagl.PLViewEventListener;
import com.panoramagl.enumeration.PLCubeFaceOrientation;
import com.panoramagl.hotspots.PLHotspot;
import com.panoramagl.ios.structs.CGPoint;
import com.panoramagl.structs.PLPosition;
import com.panoramagl.utils.PLUtils;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ZoomControls;
import android.widget.AdapterView.OnItemSelectedListener;
import android.view.View.OnClickListener;

public class ShowPanorama extends PLView
{
	/**constants*/
	
	private static final int kHotspotIdMin = 1;
	private static final int kHotspotIdMax = 1000;
	
	/**member variables*/
	AlertDialogManager alert = new AlertDialogManager();
	private Random random = new Random();
	private Bitmap bitmap = null;
	ConsumeWebService c;
	public static int panoramaId=0;
	
	/**init methods*/
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       // this.loadPanorama(0);
        c=new ConsumeWebService();
		new Create().execute();
        this.setListener(new PLViewEventListener()
        {
        	@Override
    		public void onDidClickHotspot(PLIView pView, PLHotspot hotspot, CGPoint screenPoint, PLPosition scene3DPoint)
        	{
        		Toast.makeText(pView.getActivity(), String.format("You select the hotspot with ID %d", hotspot.getIdentifier()), Toast.LENGTH_SHORT).show();
        	}
		});
    }
    
    /**
     * This event is fired when OpenGL renderer was created
     * @param gl Current OpenGL context
     */
   /* @Override
	protected void onGLContextCreated(GL10 gl)
	{
    	super.onGLContextCreated(gl);
    	
    	//Add layout
    	View mainView = this.getLayoutInflater().inflate(R.layout.activity_main, null);
        this.addContentView(mainView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
        //Spinner control
        Spinner panoramaTypeSpinner = (Spinner)mainView.findViewById(R.id.spinner_panorama_type);
        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.panorama_types, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       // panoramaTypeSpinner.setAdapter(adapter);
        panoramaTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {
			
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
			{
				loadPanoramaFromJSON(position);
			}

			
			public void onNothingSelected(AdapterView<?> parentView)
			{
			}
		});
        
        //Zoom controls
        ZoomControls zoomControls = (ZoomControls)mainView.findViewById(R.id.zoomControls);
        zoomControls.setOnZoomInClickListener(new OnClickListener()
        {	
			
			public void onClick(View view)
			{
				getCamera().zoomIn(true);
			}
		});
        zoomControls.setOnZoomOutClickListener(new OnClickListener()
        {	
		
			public void onClick(View view)
			{
				getCamera().zoomOut(true);
			}
		});
	}*/
    
    /**load methods*/
    
    /**
     * Load panorama image manually
     * @param index Spinner position where 0 = spherical, 1 = spherical2, 2 = cubic, 3 = cylindrical
     */
    private void loadPanorama(int index,String url)
    {
    	GL10 gl = this.getCurrentGL();
    	PLIPanorama panorama = null;
    	//Lock panoramic view
    	this.setBlocked(true);
    	//Spherical panorama example (supports up 1024x512 texture)
    	if (index == 0) {
			panorama = new PLSphericalPanorama();
			bitmap = DownloadImage(url);
			((PLSphericalPanorama) panorama).setImage(gl,
					PLImage.imageWithBitmap(bitmap));
		}
        //Add a hotspot
        panorama.addHotspot(new PLHotspot((kHotspotIdMin + Math.abs(random.nextInt()) % ((kHotspotIdMax + 1) - kHotspotIdMin)), PLImage.imageWithBitmap(PLUtils.getBitmap(this, R.raw.hotspot), false), 0.0f, 0.0f, 0.08f, 0.08f));
        //Load panorama
        this.reset();
        this.setPanorama(panorama);
        //Unlock panoramic view
        this.setBlocked(false);
    }
    
    private InputStream OpenHttpConnection(String urlString) throws IOException {
		InputStream in = null;
		int response = -1;

		URL url = new URL(urlString);
		URLConnection conn = url.openConnection();

		if (!(conn instanceof HttpURLConnection))
			throw new IOException("Not an HTTP connection");

		try {
			HttpURLConnection httpConn = (HttpURLConnection) conn;
			httpConn.setAllowUserInteraction(false);
			httpConn.setInstanceFollowRedirects(true);
			httpConn.setRequestMethod("GET");
			httpConn.connect();

			response = httpConn.getResponseCode();
			if (response == HttpURLConnection.HTTP_OK) {
				in = httpConn.getInputStream();
			}
		} catch (Exception ex) {
			throw new IOException("Error connecting");
		}
		return in;
	}

	private Bitmap DownloadImage(String URL) {
		Bitmap bitmap = null;
		InputStream in = null;
		try {
			in = OpenHttpConnection(URL);
			bitmap = BitmapFactory.decodeStream(in);
			in.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return bitmap;
	}
    
    /**
     * Load panorama image using JSON protocol
     * @param index Spinner position where 0 = spherical, 1 = spherical2, 2 = cubic, 3 = cylindrical
     */
    private void loadPanoramaFromJSON(int index)
    {
    	PLILoader loader = null;
    	if(index == 0)
    		loader = new PLJSONLoader(this, "res://raw/json_spherical");
    	else if(index == 1)
    		loader = new PLJSONLoader(this, "res://raw/json_spherical2");
    	else if(index == 2)
    		loader = new PLJSONLoader(this, "res://raw/json_cubic");
    	else if(index == 3)
    		loader = new PLJSONLoader(this, "res://raw/json_cylindrical");
    	this.load(loader);
    }
    
    public class Create extends AsyncTask<Void, Void, Void> {

	    private final ProgressDialog dialog = new ProgressDialog(ShowPanorama.this);
	    boolean results;
	    String data;
	    protected void onPreExecute() {

	            this.dialog.setMessage("Loading...");
	            this.dialog.show();
	            this.dialog.setCancelable(true);
	    }

	    protected Void doInBackground(final Void... unused) {
	    	
			try {
				data=c.getpanorama(CustomBalloonOverlayView.panoramaId);
			} catch (Exception e) {
				Log.e("","",e);
				Toast.makeText(getApplicationContext(), "No Panorama Image Found!", Toast.LENGTH_LONG).show();
			}
	   
	        return null; 
	    }
	    
	    protected void onPostExecute(Void result) {
	    	
	    	if (this.dialog!=null) {
	            this.dialog.dismiss();
	            } 
	    	Toast.makeText(getApplicationContext(), ""+data, Toast.LENGTH_LONG).show();
	    	if(data.length()!=0){
			runOnUiThread(new Runnable(){
				public void run() {	
					loadPanorama(0,data);
				}
			});

	    	}else if(data.length()==0){
	    		alert.showAlertDialog(ShowPanorama.this, "NO DATA FOUND",
						"Sorry no data found. Try to check connection",
						false);	    		
	    	}
	    	
	    	
	}
    }
}
